﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLTuan.App.AccessLayer;
using TLTuan.App.TransferObj;

namespace TLTuan.Home
{
    public partial class SuaThongTinThanhVien : System.Web.UI.Page
    {
        #region Menber
        CThanhVien m_c_thanhvien;
        CDAOThanhVien m_dao_thanhvien;
        #endregion

        #region Interface ...

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["user"] == null)
                {
                    Response.Redirect("../Home/Default.aspx");
                }
                else
                {
                    load_cbo_datetime();
                    load_thongtinthanhvien();
                }
            }
        }

        protected void Btt_huy_Click(object sender, EventArgs e)
        {
            DataTable table = (DataTable)Session["ThanhVien"];

            if (table != null && table.Rows.Count > 0)
            {
                Response.Redirect("../Home/MyProfile.aspx?user=" + table.Rows[0]["PK_ThanhvienID"].ToString());
            }
            else
            {
                Response.Redirect("../Home/Default.aspx");
            }
        }

        protected void btt_dangky_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable table = (DataTable)Session["ThanhVien"];
                
                if (table != null && table.Rows.Count > 0)
                {
                    if (table.Rows[0]["PK_ThanhvienID"].ToString() == Request.QueryString["user"].ToString())
                    {
                        if (txt_user.Text != "" && txt_pass.Text != "" && txt_pass2.Text != "")
                        {
                            
                            m_dao_thanhvien = new CDAOThanhVien();
                            DateTime d = new DateTime(int.Parse(year.SelectedValue.ToString()), int.Parse(month.SelectedValue.ToString()), int.Parse(date.SelectedValue.ToString()));
                            m_c_thanhvien = m_dao_thanhvien.SelectOne(int.Parse(txt_user.ToolTip));                            
                            m_c_thanhvien.TenThanhVien = txthoten.Text;
                            m_c_thanhvien.NgaySinh = d;
                            m_c_thanhvien.GioiTinh = rdl_gender_check.SelectedValue.ToString() == "Male" ? true : false;
                            m_c_thanhvien.Mobile = txt_phone.Text;
                            m_c_thanhvien.Email = txt_email.Text;
                            if (txt_pass.Text != "")
                            {
                                if (txt_pass.Text != txt_pass2.Text)
                                {
                                    lab_thongbao.Text = "Mật khẩu không giống nhau!";
                                    lab_thongbao.Visible = true;
                                    return;
                                }
                                m_c_thanhvien.Password = txt_pass.Text;
                            }
                            m_dao_thanhvien.Update(m_c_thanhvien);
                            Response.Redirect("../Home/MyProfile.aspx?user=" + m_c_thanhvien.PK_ThanhVienID.ToString());
                        }
                        else
                        {
                            lab_thongbao.Text = "Bạn chưa điền đủ các thông tin băt buộc!";
                            lab_thongbao.Visible = true;
                            return;
                        }
                    }
                    else
                    {
                        lab_thongbao.Text = "Bạn hãy đăng nhập lại để sửa thông tin!";
                        lab_thongbao.Visible = true;
                        return;
                    }
                }                
            }
            catch (Exception ex)
            {
                lab_thongbao.Text = ex.Message;
                lab_thongbao.Visible = true;
            }
        }
        #endregion

        #region Method

        private void load_thongtinthanhvien()
        {
            m_dao_thanhvien = new CDAOThanhVien();
            m_c_thanhvien = m_dao_thanhvien.SelectOne(int.Parse(Request.QueryString["user"].ToString()));
            if (m_c_thanhvien!=null)
            {
                txt_user.Text = m_c_thanhvien.TaiKhoan;
                txt_user.ToolTip = m_c_thanhvien.PK_ThanhVienID.ToString();
                txthoten.Text = m_c_thanhvien.TenThanhVien;
                txt_phone.Text = m_c_thanhvien.Mobile;
                txt_email.Text = m_c_thanhvien.Email;
                date.SelectedValue = m_c_thanhvien.NgaySinh.Day.ToString();
                month.SelectedValue = m_c_thanhvien.NgaySinh.Month.ToString();
                year.SelectedValue = m_c_thanhvien.NgaySinh.Year.ToString();
                rdl_gender_check.SelectedValue = m_c_thanhvien.GioiTinh == true ? "Male" : "Female";
            }
        }

        private void load_cbo_datetime()
        {
            ListItem item;
            for (int i = 1; i < 31; i++)
            {
                item = new ListItem("Ngày " + i.ToString(), i.ToString());
                date.Items.Add(item);
            }

            for (int i = 1; i < 13; i++)
            {
                item = new ListItem("Tháng " + i.ToString(), i.ToString());
                month.Items.Add(item);
            }

            for (int i = 1900; i < 2020; i++)
            {
                item = new ListItem(i.ToString(), i.ToString());
                year.Items.Add(item);
            }

            year.SelectedValue = DateTime.Today.Year.ToString();
        }

        #endregion

        
    }
}